<?php
declare(strict_types=1);
namespace Cache;
use Config;
use JetBrains\PhpStorm\NoReturn;
use Yaf\Exception\TypeError;

class Memcached
{
    /**
     * 静态实例
     * @var \Memcached|null
     */
    private static ?\Memcached $_instance = null;


    /**
     */
    #[NoReturn] private function __construct(){
        self::$_instance = new \Memcached();
        $config = Config::get("memcached");
        self::$_instance->addServer($config['host'],(int)$config['port'],33);
        self::$_instance->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_PHP);
    }

    /**
     * 获取静态实例
     * @return \Memcached|null
     */
    public static function getMemcached(): ?\Memcached
    {
        if(!self::$_instance) new self();
        return self::$_instance;
    }

    private function __clone(){}
}
